﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DXApplicationTangche.UC.项目成果管理.在制品管理.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;

namespace DXApplicationTangche.UC.项目成果管理.在制品管理
{
    public partial class FormVI质检新 : DevExpress.XtraEditors.XtraForm
    {
        private String code;    //  工序编码
        private String barcode { get; set; }//水洗标
        public 质检Model model;
        private OrderCommitDTO返回 orderCommitDTO返回 = new OrderCommitDTO返回();
        private QcBarcodeItemsDTO qcBarcodeItems = new QcBarcodeItemsDTO();
        public FormVI质检新(String code)
        {
            InitializeComponent();
            this.code = code;
            this.ActiveControl = this.textBox扫描水洗标;
        }
        private void textBox扫描水洗标_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox扫描水洗标.Text.Trim()))
                {
                    return;
                }
                this.barcode = this.textBox扫描水洗标.Text.Trim();
                try
                {
                    this.qcBarcodeItems = 在制品管理Service.getQcItemsDTOByBarcode(this.barcode);
                    this.orderCommitDTO返回 = 订单预览新Service.getOrderCommitDTOByBarcode(this.barcode);
                }
                catch
                {
                    MessageBox.Show("无查询信息");
                }
                this.model = new 质检Model(qcBarcodeItems);
                this.显示();
                this.刷新显示半成品量尺();
                this.刷新显示半成品设计点();
            }
        }
        private void simpleButton刷新_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox扫描水洗标.Text.Trim()))
            {
                return;
            }
            this.barcode = this.textBox扫描水洗标.Text.Trim();
            try
            {
                this.qcBarcodeItems = 在制品管理Service.getQcItemsDTOByBarcode(this.barcode);
                this.orderCommitDTO返回 = 订单预览新Service.getOrderCommitDTOByBarcode(this.barcode);
            }
            catch
            {
                MessageBox.Show("无查询信息");
            }
            this.model = new 质检Model(qcBarcodeItems);
            this.显示();
            this.刷新显示半成品量尺();
            this.刷新显示半成品设计点();
        }
        private void 显示()
        {
            this.richTextBox备注.Text = this.qcBarcodeItems.remarks;
            this.textBox款式.Text = this.qcBarcodeItems.styleNameCn;
            try { this.textBox尺码.Text = this.qcBarcodeItems.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { this.textBox尺码.Text = ""; }
            this.textBox面料名称.Text = this.qcBarcodeItems.materialNameCn;
            this.textBox面料编码.Text = this.qcBarcodeItems.materialCode;
            this.pictureBox款式.LoadAsync(URL.图片url + URL.款式图片路径 + this.qcBarcodeItems.styleImagePath);
            this.pictureBox面料.LoadAsync(URL.图片url + URL.面料图片路径 + this.qcBarcodeItems.materialImagePath);
            this.textBox订单时间.Text = Convert.ToDateTime(this.orderCommitDTO返回.orderDate).ToString();
            this.textBox客户名.Text = this.orderCommitDTO返回.customerName;
            this.textBox订单号.Text = this.orderCommitDTO返回.orderNo;
        }

        /// <summary>
        /// 检查尺寸通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            try
            {
                String id = this.treeList半成品量尺.FocusedNode["id"].ToString();
                //  尺寸通过
                在制品管理Service.inspectSizePassByBarcode(this.barcode,id);
                //  改变页面显示
                this.model.build半成品量尺通过(id);
                //  刷新
                //this.刷新显示半成品量尺();
                this.treeList半成品量尺.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 检查设计点通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemButtonEdit设计点_Click(object sender, EventArgs e)
        {
            try
            {
                String itemCd =
                    FunctionHelper.GetgridViewValue(this.gridView半成品设计点, this.gridView半成品设计点.FocusedRowHandle, "itemCd");
                String itemValue =
                    FunctionHelper.GetgridViewValue(this.gridView半成品设计点, this.gridView半成品设计点.FocusedRowHandle, "itemValue");
                //  设计点通过
                在制品管理Service.inspectDesignItemPassByBarcode(
                        this.barcode,
                        this.model.build取单行设计点(itemCd, itemValue)
                    );
                //  改变页面显示
                this.model.build半成品设计点通过(itemCd, itemValue);
                //  刷新
                this.刷新显示半成品设计点();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 刷新显示半成品量尺()
        {
            //转换显示
            this.treeList半成品量尺.DataSource = this.model.list半成品量尺呈现;
            this.treeList半成品量尺.RefreshDataSource();
            this.treeList半成品量尺.ExpandAll();
        }
        private void 刷新显示半成品设计点()
        {
            //转换显示
            this.gridControl半成品设计点.DataSource = this.model.list半成品设计点呈现;
            this.gridView半成品设计点.RefreshData();
        }

        private void treeList半成品量尺_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Column.Name == "treeListColumnIfPass")
            {
                InspectSizeVO row = this.treeList半成品量尺.GetDataRecordByNode(e.Node) as InspectSizeVO;
                if (!row.ifPass)
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                }
                else if (row.ifPass)
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
                //if (row.ifPass.Equals("未通过"))
                //{
                //    e.Appearance.BackColor = Color.Red;
                //    e.Appearance.ForeColor = Color.White;
                //}
                //else if (row.ifPass.Equals("通过"))
                //{
                //    e.Appearance.BackColor = Color.Green;
                //    e.Appearance.ForeColor = Color.White;
                //}
            }
        }

        private void gridView半成品设计点_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumnifPass")
            {
                if (e.CellValue.Equals("未通过"))
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                }
                else if (e.CellValue.Equals("通过"))
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
            }
        }

        private void simpleButton提交_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.barcode)
                    || string.IsNullOrEmpty(this.code)
                    )
            {
                MessageBox.Show("无扫描内容");
                return;
            }
            new FormVI质检提交新(this.barcode,this.code).ShowDialog();
            this.textBox扫描水洗标.Clear();
            this.textBox扫描水洗标.Focus();
        }

        private void simpleButton清空水洗标_Click(object sender, EventArgs e)
        {
            this.textBox扫描水洗标.Clear();
            this.ActiveControl = this.textBox扫描水洗标;
        }

    }
    public class 质检Model
    {
        public List<InspectSizeVO> list半成品量尺呈现 = new List<InspectSizeVO>();
        public List<DesignItemCheckVO> list半成品设计点呈现 = new List<DesignItemCheckVO>();
        public 质检Model(QcBarcodeItemsDTO qcBarcodeItems)
        {
            //尺寸
            this.list半成品量尺呈现.Clear();
            foreach (InspectSizeDTO dTO in qcBarcodeItems.inspectSizeDTOS)
            {
                this.list半成品量尺呈现.Add(new InspectSizeVO(dTO));
            }
            this.list半成品量尺呈现.Sort(new InspectSizeVOIndexComparer());
            //设计点
            this.list半成品设计点呈现.Clear();
            foreach (DesignItemCheckDTO dTO in qcBarcodeItems.designItemCheckDTOS)
            {
                this.list半成品设计点呈现.Add(new DesignItemCheckVO(dTO));
            }
        }
        /// <summary>
        /// 半成品量尺根据index排序
        /// </summary>
        public class InspectSizeVOIndexComparer : IComparer<InspectSizeVO>
        {
            public int Compare(InspectSizeVO p1, InspectSizeVO p2)
            {
                return p1.index.CompareTo(p2.index);
            }
        }

        public 质检Model build半成品量尺通过(String id)
        {
            foreach (InspectSizeVO dTO in this.list半成品量尺呈现)
            {
                if (id.Equals(dTO.id))
                {
                    if (dTO.ifPass)
                    {
                        dTO.ifPass = false;
                        dTO.ifPassText = "未通过";
                    }
                    else
                    {
                        dTO.ifPass = true;
                        dTO.ifPassText = "通过";
                    }
                }
            }
            return this;
        }
        public 质检Model build半成品量尺通过All(String barcode)
        {
            foreach (InspectSizeVO dTO in this.list半成品量尺呈现)
            {
                if (!dTO.ifPass)
                {
                    dTO.ifPass = true;
                    dTO.ifPassText = "通过";
                    //  尺寸通过
                    在制品管理Service.inspectSizePassByBarcode(barcode, dTO.id);
                }
            }
            return this;
        }

        public DesignItemCheckDTO build取单行设计点(string itemCd, String itemValue)
        {
            foreach (DesignItemCheckVO dTO in this.list半成品设计点呈现)
            {
                if (itemCd.Equals(dTO.itemCd) && itemValue.Equals(dTO.itemValue))
                {
                    return new DesignItemCheckDTO(dTO);
                }
            }
            return new DesignItemCheckDTO();
        }

        public 质检Model build半成品设计点通过(string itemCd, String itemValue)
        {
            foreach (DesignItemCheckVO dTO in this.list半成品设计点呈现)
            {
                if (itemCd.Equals(dTO.itemCd) && itemValue.Equals(dTO.itemValue))
                {
                    if (dTO.ifPass)
                    {
                        dTO.ifPass = false;
                        dTO.ifPassText = "未通过";
                    }
                    else
                    {
                        dTO.ifPass = true;
                        dTO.ifPassText = "通过";
                    }
                }
            }
            return this;
        }
        public 质检Model build半成品设计点通过All(String barcode)
        {
            foreach (DesignItemCheckVO dTO in this.list半成品设计点呈现)
            {
                if (!dTO.ifPass)
                {
                    dTO.ifPass = true;
                    dTO.ifPassText = "通过";
                    //  设计点通过
                    在制品管理Service.inspectDesignItemPassByBarcode(
                            barcode,
                            new DesignItemCheckDTO(dTO)
                        );
                }
            }
            return this;
        }
    }
    /// <summary>
    /// 仅用于页面显示
    /// </summary>
    public class InspectSizeVO
    {
        public String id { get; set; }//id
        public int index { get; set; }//序号
        public String itemNameCn { get; set; }//大分类名称
        public Double garmentSize { get; set; }//成衣尺寸
        public String parentId { get; set; }//父id
        public Boolean ifPass { get; set; }//是否通过
        public String ifPassText { get; set; }//是否通过
        public Boolean hasFitValue { get; set; }//是否人尺寸
        public Boolean hasGarmentSize { get; set; }//是否成衣尺寸
        public Boolean hasHalf { get; set; }//是否1/2

        public InspectSizeVO(InspectSizeDTO dTO)
        {
            this.id = dTO.id;
            this.index = dTO.index;
            this.itemNameCn = dTO.itemNameCn;
            this.garmentSize = dTO.garmentSize;
            this.parentId = dTO.parentId;
            this.ifPass = dTO.ifPass;
            this.ifPassText = dTO.ifPass ? "通过" : "未通过";
            this.hasFitValue = dTO.hasFitValue;
            this.hasGarmentSize = dTO.hasGarmentSize;
            this.hasHalf = dTO.hasHalf;
        }
    }
    /// <summary>
    /// 仅用于页面显示
    /// </summary>
    public class DesignItemCheckVO
    {
        public String itemTypeNameCn { get; set; }//
        public String itemCd { get; set; }//
        public String itemValue { get; set; }//
        public String optionValue { get; set; }//
        public String name { get; set; }//设计点名称
        public Boolean ifPass { get; set; }//是否通过
        public String ifPassText { get; set; }//是否通过
        public int index { get; set; }//序号
        public DesignItemCheckVO(DesignItemCheckDTO dTO)
        {
            this.itemTypeNameCn = dTO.itemTypeNameCn;
            this.itemCd = dTO.itemCd;
            this.itemValue = dTO.itemValue;
            this.optionValue = dTO.optionValue;
            this.name = dTO.name;
            this.ifPass = dTO.ifPass;
            this.ifPassText = dTO.ifPass ? "通过" : "未通过";
            this.index = dTO.index;
        }
    }
}